package date0704pm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcTest24 {

	public static void main(String[] args) throws Exception {

		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:ecut";
		String user = "ecuter";
		String password = "ecuter";

//		String driver = "com.mysql.cj.jdbc.Driver"  ;
//		String url = "jdbc:mysql://localhost:3306/ecut?serverTimezone=Asia/Shanghai&useSSL=false" ;
//		String user = "ecuter" ;
//		String password = "ecuter" ;

		// 1、加载数据库驱动
		Class.forName(driver);
		// 2、获得数据库连接
		Connection connection = DriverManager.getConnection(url, user, password);

		int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
		int resultSetConcurrency = ResultSet.CONCUR_UPDATABLE;
		// 3、创建 Statement对象 ( 指定结果集的 滚动特性 和 并发特性 )
		Statement statement = connection.createStatement(resultSetType, resultSetConcurrency);

		ResultSet rs = statement.executeQuery("SELECT rownum , empno , ename , job , sal FROM emp");

		// while循环前，结果集指针在 第一行数据之前
		while (rs.next()) {
			System.out.print(rs.getInt("rownum") + ":\t");
			System.out.print(rs.getInt("empno") + "\t");
			System.out.print(rs.getString("ename") + "\t");
			System.out.print(rs.getString("job") + "\t");
			System.out.println(rs.getFloat("sal"));
		} // while循环结束时，结果集指针在 最后一行数据之后

		System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");

		// while循环前，结果集指针在最后一行数据之后
		while (rs.previous()) { // 判断是否存在前一行数据
			System.out.print(rs.getInt("rownum") + ":\t");
			System.out.print(rs.getInt("empno") + "\t");
			System.out.print(rs.getString("ename") + "\t");
			System.out.print(rs.getString("job") + "\t");
			System.out.println(rs.getFloat("sal"));
		} // while循环结束时，结果集指针在 第一行数据之前

		System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");

		// 使用绝对定位
		rs.absolute(10);

		while (rs.next()) {
			System.out.print(rs.getInt("rownum") + ":\t");
			System.out.print(rs.getInt("empno") + "\t");
			System.out.print(rs.getString("ename") + "\t");
			System.out.print(rs.getString("job") + "\t");
			System.out.println(rs.getFloat("sal"));
		}

		System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");

		// 使用绝对定位
		rs.absolute(10);

		// while循环前，结果集指针在最后一行数据之后
		while (rs.previous()) { // 判断是否存在前一行数据
			System.out.print(rs.getInt("rownum") + ":\t");
			System.out.print(rs.getInt("empno") + "\t");
			System.out.print(rs.getString("ename") + "\t");
			System.out.print(rs.getString("job") + "\t");
			System.out.println(rs.getFloat("sal"));
		}

		// 6、释放资源( 所有已经开启的资源必须都关闭 )
		rs.close();
		statement.close();
		connection.close();

	}

}
